/*
 * VegetableSupply.cpp
 *
 *  Created on: 09-01-2011
 *      Author: Piotr
 */

#include "world/VegetableSupply.hpp"
#include "world/ElementsVisitor.hpp"
#include "utils/Randomizer.hpp"

const float VegetableSupply::CHANCE_TO_GROW = 0.006f;
const float VegetableSupply::CHANCE_TO_SPREAD = 0.0005f;

VegetableSupply::VegetableSupply() :
	Supply()
{
}

VegetableSupply::VegetableSupply(const VegetableSupply& other) :
	Supply(other)
{
}

VegetableSupply::VegetableSupply(const Vector& position) :
	Supply(position)
{
}

void VegetableSupply::updateWithDeltaTime(float deltaTimeInSeconds)
{
	Supply::updateWithDeltaTime(deltaTimeInSeconds);

	if(!isEmpty() && Randomizer::nextUniformFloat() < CHANCE_TO_SPREAD)
	{
		int x = Randomizer::nextInt(-1, 1);
		int y = Randomizer::nextInt(-1, 1);
		x *= VegetableSupply::SIZE;
		y *= VegetableSupply::SIZE;
		const Vector& spreadVector = Vector(x, y);
		const Dimension& size = Dimension(VegetableSupply::SIZE, VegetableSupply::SIZE);

		const Vector& newPosition = getPosition() + spreadVector;
		const geometry::Rectangle& bounds = geometry::Rectangle(newPosition, size);
		if(World::getInstance().canPlaceAt(bounds))
		{
			World::getInstance().scheduleToAdd(new VegetableSupply(newPosition));
		}
	}
}

void VegetableSupply::accept(ElementsVisitor& visitor)
{
	visitor.visit(*this);
}
